package servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import custom.Question;
import custom.Quiz;

/**
 * Servlet implementation class QuizEditPushedServlet
 */
@WebServlet("/QuizEditPushedServlet")
public class QuizEditPushedServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QuizEditPushedServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		Quiz qz = null;
		try {
			qz = Quiz.getInstance(name);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		
		request.getSession().setAttribute("questions", qz.getQuestionArrayList());
		request.getSession().setAttribute("quizName", name);
		request.getSession().setAttribute("quizDesc", qz.getDesc());
		
		Quiz.removeQuiz(qz.getId());
		
		RequestDispatcher dispatch = request.getRequestDispatcher("editQuiz.jsp");
		dispatch.forward(request, response);
	}
}
